// import Cookies from 'js-cookie'

const tokenKey = 'Z-Token'
const menuListKey = 'MENU_LIST'
const userKey = 'CURRENT_USER'

interface Menu {
  enName: string,
}

export function getToken() {
  return localStorage.getItem(tokenKey)
  // return Cookies.get(TokenKey)
}

export function setToken(token: string) {
  return localStorage.setItem(tokenKey, token)
  // return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return localStorage.removeItem(tokenKey)
}

export function getMenuList() {
  return localStorage.getItem(menuListKey)
}

export function setMenuList(menuList: Menu[]) {
  return localStorage.setItem(menuListKey, JSON.stringify(menuList))
}

export function setMenuListStr(menuList: string) {
  return localStorage.setItem(menuListKey, menuList)
}

export function removeMenuList() {
  return localStorage.removeItem(menuListKey)
}

export function removeAllCache() {
  removeToken();
  removeMenuList();
}

export function setUserInfo(userInfo: string) {
  return localStorage.setItem(userKey, userInfo)
}

// 返回当前用户信息，json格式
// 使用例子：
// import { removeAllCache, setUserInfo, getUserInfo } from "@/utils/auth";
// console.log(res.id, res.name, res.phone)
export function getUserInfo() {
  let userInfoBase64 = localStorage.getItem(userKey) as string;
  if (!userInfoBase64) {
    return null;
  }
  let result = atob(userInfoBase64)
  const utf8BytesRes = new Uint8Array(result.split('').map(char => char.charCodeAt(0)));
  const jsonStrRes = new TextDecoder().decode(utf8BytesRes) as string;
  return JSON.parse(jsonStrRes)
}